<?php

echo "<img src = \"coursehead.jpg\" alt = \"Courses!\" /><br>";

function generateCourseList() {
    $courseArray = array();
    $courseArray = courseManager::getAllCourses($_SESSION['userID']);
    $default = "<table>";
    foreach ($courseArray as $course) {
        $grade = $course->calculateAverage();
        $default = $default . "<tr><td valign=\"top\">" . $course->getName() . "</td><td valign=\"top\">$grade%</td><td valign=\"top\"><a href=\"index.php?add=assign&courseid=" . $course->getID() . "\">Add Assignment</a></td><td valign=\"top\"><a href=\"index.php?edit=course&id=" . $course->getID() . "\">Edit</a></td></tr>";
    }
    $default = $default . "</table><br><br>";

    $default = $default . "<a href=index.php?add=course>Add Course</a>";

    return $default;
}

function generateProfessors() {
    $profArray = array();
    $profArray = professorManager::getProfessors($_SESSION['userID']);
    $default = "<select name=\"professorID\">";
    foreach ($profArray as $professor) {
        $ID = $professor->getID();
        $name = $professor->getName();
        $default = $default . "<option value=\"$ID\">$name</option>";
    }
    $default = $default . "</select>";
    return $default;
}

if ($add == "course") {
    $genProf = generateProfessors();
    $courseForm = "
        <form method=\"POST\" action=\"index.php?add=course&submit=1\">
        <table>
        <tr><td>*Course name:</td><td><input type=\"text\" name=\"name\"></td></tr>
        <tr><td>Building:</td><td><input type=\"text\" name=\"building\"></td></tr>
        <tr><td>Room number:</td><td><input type=\"text\" name=\"room\"></td></tr>
        <tr><td>Professor:</td><td><div id=\"choice\"><input type=\"radio\" name=\"professor\" id=\"professor\" value=\"add\" onclick=\"enableAdd()\"> Add or
        <input type=\"radio\" name=\"professor\" id=\"professor\" value=\"choose\" onclick=\"enableChoose()\"> Choose Existing</div></td></tr>
        </table>
        <div id=\"addProf\" style=\"display:none\">
        </center>Name: <input type=\"text\" name=\"profName\"><br>
        Office Location: <input type=\"text\" name=\"profLocation\">

        </div>
        <div id=\"chooseProf\" style=\"display:none\">
        $genProf
        </div>
        <table>
        <tr><td>Start date:</td><td><input type=\"text\" name=\"startMonth\" value=\"MM\" size=\"2\"><input type=\"text\" name=\"startDay\" value=\"DD\" size=\"2\"><input type=\"text\" name=\"startYear\" value=\"YYYY\" size=\"4\"></td></tr>
        <tr><td>End date:</td><td><input type=\"text\" name=\"endMonth\" value=\"MM\" size=\"2\"><input type=\"text\" name=\"endDay\" value=\"DD\" size=\"2\"><input type=\"text\" name=\"endYear\" value=\"YYYY\" size=\"4\"></td></tr>
        </table>
        <input type=\"submit\" name=\"submit\" value=\"Submit\">
        </form>
        ";
    if ($submit == "1") {
        if (isset($_POST['name'])) {
            $newCourse = new Course();
            $name = $_POST[name];
            $newCourse->setName($name);
            $newCourse->setBuilding($_POST[building]);
            $newCourse->setRoom($_POST[room]);

            if ($_POST['professor'] == "add")
            {
                $professor = new Professor();
                $professor->setLocation($_POST['profLocation']);
                $professor->setName($_POST['profName']);
                $professor->setUserID($_SESSION['userID']);
                $professor = professorManager::addProfessor($professor);
                $profID = $professor->getID();
            } else if ($_POST['professor'] == "choose")
            {
                $profID = $_POST['professorID'];
            }

            $newCourse->setProfessorID($profID);
            $newCourse->setStartDate($_POST[startYear] . "-" . $_POST['startMonth'] . "-" . $_POST['startDay']);
            $newCourse->setEndDate($_POST[endYear] . "-" . $_POST['endMonth'] . "-" . $_POST['endDay']);
            $newCourse->setUserID($_SESSION[userID]);
            courseManager::addCourse($newCourse);
            echo "<br><br>";
            $default = generateCourseList();
            echo $default;
        } else {
            echo "You must enter a name for the course.";
            echo $courseForm;
        }
    } else {
        echo $courseForm;
    }
} else if ($edit == "course") {
    if (isset($_GET['id'])) {

        $course = new Course();
        $course = courseManager::getCourse($_GET['id']);
        $name = $course->getName();
        $building = $course->getBuilding();
        $room = $course->getRoom();
        $professor = $course->getProfessorID();
        $courseForm = "
            <a href=\"index.php?delete=course&id=$_GET[id]\">Delete Course</a><br><br>
            or Edit Course using this form:<br>
            <form method=\"POST\" action=\"index.php?edit=course&id=$_GET[id]&submit=1\">
            <table>
            <tr><td>Course name:</td><td><input type=\"text\" name=\"name\" value=\"$name\"></td></tr>
            <tr><td>Building:</td><td><input type=\"text\" name=\"building\" value=\"$building\"></td></tr>
            <tr><td>Room number:</td><td><input type=\"text\" name=\"room\" value=\"$room\"></td></tr>
            <tr><td>Professor:</td><td><select name=\"professor\"><option value=\"1\">Default Professor</option></select></td></tr>
            <tr><td>Start date:</td><td><input type=\"text\" name=\"startMonth\" value=\"MM\" size=\"2\"><input type=\"text\" name=\"startDay\" value=\"DD\" size=\"2\"><input type=\"text\" name=\"startYear\" value=\"YYYY\" size=\"4\"></td></tr>
            <tr><td>End date:</td><td><input type=\"text\" name=\"endMonth\" value=\"MM\" size=\"2\"><input type=\"text\" name=\"endDay\" value=\"DD\" size=\"2\"><input type=\"text\" name=\"endYear\" value=\"YYYY\" size=\"4\"></td></tr>
            </table>
            <input type=\"submit\" name=\"submit\" value=\"Submit\">
            </form>
            ";

        if ($submit == "1") {
            if (isset($_POST['name'])) {
                $editCourse = new Course();
                $name = $_POST[name];
                $editCourse->setID($_GET['id']);
                $editCourse->setName($name);
                $editCourse->setBuilding($_POST['building']);
                $editCourse->setRoom($_POST['room']);
                $editCourse->setProfessorID($_POST['professor']);
                $editCourse->setStartDate($_POST['startYear'] . "-" . $_POST['startMonth'] . "-" . $_POST['startDay']);
                $editCourse->setEndDate($_POST['endYear'] . "-" . $_POST['endMonth'] . "-" . $_POST['endDay']);
                $editCourse->setUserID($_SESSION['userID']);
                courseManager::editCourse($editCourse);
                echo "<br><br>";
                $default = generateCourseList();
                echo $default;
            } else {
                echo "<p>You must enter a name for the course.</p>";
                echo $courseForm;
            }
        } else {
            echo $courseForm;
        }
    } else {
        echo "<p>You must select a course to edit.</p>";
        echo $courseForm;
    }
} else if ($delete == "course") {
    if (isset($_GET['id'])) {
        $courseDelete = courseManager::getCourse($_GET['id']);
        if ($submit == "1") {
            courseManager::deleteCourse($courseDelete);
            $default = generateCourseList();
            echo $default;
        } else {
            echo "<p>Are you sure you wish to delete this course?</p><br><br>";
            $name = $courseDelete->getName();
            $professor = $courseDelete->getProfessorID();
            $building = $courseDelete->getBuilding();
            $room = $courseDelete->getRoom();
            echo "
                $name <br>
                $professor <br>
                $building $room <br><br>
                <a href=\"index.php?delete=course&id=$_GET[id]&submit=1\">Yes</a> or <a href=\"index.php\">No</a>
                ";
        }
    } else {
        echo "<p>You must select a course to delete.</p>";
        echo $default;
    }
} else {
    $default = generateCourseList();
    echo $default;
}
?>
